package com.school.controller.admin;

import com.school.common.ServiceResultEnum;
import com.school.entity.Coupon;
import com.school.service.CouponService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/admin")
public class CouponController {
    @Resource
    private CouponService couponService;
    @GetMapping("/coupon")
    public String couponPage(HttpServletRequest request) {
        request.setAttribute("path", "tea_shop_coupon");
        return "admin/tea_shop_coupon";
    }
    @GetMapping("/coupon/list")
    @ResponseBody
    public Result list(@RequestParam Map<String,Object> params){
        if(StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
            return ResultGenerator.genFailResult("参数异常");
        }
        //分页操作
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        //创建业务层对象，根据业务层提供的业务方法做查询操作
        PageResult pageResult=couponService.getCouponPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }
    @PostMapping("/coupon/save")
    @ResponseBody
    public Result save(@RequestBody Coupon coupon){
        if(coupon.getCouponName()==null||coupon.getCouponName().length()>100){
            return ResultGenerator.genFailResult("参数异常");
        }
        String result=couponService.saveCoupon(coupon);
        if(ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genFailResult(result);
        }
    }
}
